﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using brockredge.CLASSES;
using System.Data.SqlClient;
using System.Data;
using System.Web.Security;
using System.Configuration;

namespace brockredge
{
    public partial class Welcome : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            //if (Session["userId"] == null)
            //{
            //    Response.Redirect("/Default.aspx");
            //}
            //if (Page.IsPostBack)
            //{
            //    return;
            //}
            //welcome.Text = Session["userId"].ToString();
            
                    
            //ddlcity.Attributes.Add("OnChange", "javascript:fillAreaDropDownBoxAndCityOther(this)");
            ////otherCity.Visible = false;
            //Areadl.Attributes.Add("OnChange","javascript:areaOther(this)");
            ////otherArea.Visible = false;
            //ddlstate.Attributes.Add("OnChange", "javascript:fillCityDropDownBox($(this).val(),"+ddlcity.ClientID+")");
            //fillStateDropDownBox();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                Guid add_id = System.Guid.NewGuid();
                Guid user_id = (Guid)Session["userId"].ToString().ToGuid();
                Guid locationId = System.Guid.NewGuid();
                testDataContext tdc = new testDataContext();
                if (CreateAdvertisement(add_id, locationId, tdc))
                {
                    if (InsertInUserAdvertisement(add_id, user_id, tdc))
                        Response.Redirect("/Welcome.aspx?u=T");
                }
            }
            catch (Exception ex)
            {
                errorlbl.Text = "Sorry Server is busy! Please try again later.";
            }
                     
        }


        protected bool CreateAdvertisement(Guid add_id,Guid locId,testDataContext tdc)
        {
            try
            {
                if (InsertInAdvertismentLocation(add_id, locId, tdc))
                {
                    Advertisement ad = new Advertisement();

                    ad.add_id = add_id;
                    ad.title = titletxtbox.Text;
                    if (rentchkbox.Checked == true)
                        ad.IsForRent = 1;
                    else
                        ad.IsForRent = 0;
                    if (sellchkbox.Checked == true)
                        ad.IsForSell = 1;
                    else
                        ad.IsForSell = 0;
                    ad.cost_for_rent = (Decimal)rentcosttxtbox.Text.ToDouble();
                    ad.cost_for_sell = (Decimal)sellcosttxtbox.Text.ToDouble();
                    ad.createDate = DateTime.Today.Date;
                    ad.description = descriptiontxtbox.Text;
                    ad.Location_id = locId;


                    tdc.Advertisements.InsertOnSubmit(ad);
                    tdc.SubmitChanges();
                }
            }
            catch (SqlException sqle)
            {
                return false;
            }
            return true;
        }

        private bool InsertInAdvertismentLocation(Guid add_id, Guid locId, testDataContext tdc)
        {
            try
            {
                location lo = new location();
                lo.location_id = locId;
                lo.location_name = locnametxtbox.Text;
                lo.landmark = landmarktxtbox.Text;
                lo.isapproved = 0;

                tdc.locations.InsertOnSubmit(lo);
                tdc.SubmitChanges();

                /** Insert in location_city table*/
                Location_City lc = new Location_City();
                lc.location_id = locId;
                lc.city_id = (Guid)ddlcity.Text.ToGuid();

                tdc.Location_Cities.InsertOnSubmit(lc);
                tdc.SubmitChanges();
                return true;
            }
            catch (SqlException sqle)
            {
                return false;
            }        
        }

        protected bool InsertInUserAdvertisement(Guid add_id, Guid user_id, testDataContext tdc)
        {
            try
            {
                User_Advertisement ud = new User_Advertisement();
                ud.add_id = add_id;
                ud.user_id = user_id;
                ud.IsApproved = 0;

                tdc.User_Advertisements.InsertOnSubmit(ud);
                tdc.SubmitChanges();
            }
            catch (SqlException sqle)
            {
                return false;
            }

            return true;
        }
    }
}